package Application.Controller;

import Application.AppJoint;
import Application.Color.BlendLayer;
import Application.File.FileAdd;
import Application.File.FileSave;
import Application.Layer.Layer;
import Application.Mirrors.*;
import Application.StatusBarManager.StatusBarManager;
import javafx.collections.ObservableList;
import javafx.embed.swing.SwingFXUtils;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.*;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import javafx.scene.Node;
import Application.Tool.*;
import Application.Layer.LayerDelegate;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public class MainWindowController extends Controller {
    // 工具设置tab
    @FXML
    private ScrollPane tabToolParam;
    // 显示/隐藏图层按钮
    @FXML
    private Button btnShowLayer;

    public VBox getVboxLayer() {
        return vboxLayer;
    }

    public VBox getVboxToolBar() {
        return vboxToolBar;
    }

    //toolbar的vbox
    @FXML VBox vboxToolBar;

    // 图层vbox
    @FXML
    private VBox vboxLayer;
    // 视口
    @FXML
    private ImageView viewport;
    @FXML
    private GridPane gridview;
    @FXML
    private Label statusBar;

    public GridPane getGridview() {
        return gridview;
    }

    public ImageView getViewport() {
        return viewport;
    }

    public ImageView getImageView() {
        return viewport;
    }

    public Label getStatusBar() {
        return statusBar;
    }

    private void setLayerActive(int id, GridPane root) {
        // 设置上一个被选中的为未选中色
        int index = LayerDelegate.getInstance().getIndexGivenId(AppJoint.getInstance().getActiveLayerId());
        if (index != -1) {
            GridPane pane = (GridPane) vboxLayer.getChildren().get(index);
//            GridPane pane = (GridPane)vboxLayer.
            pane.setBackground(new Background(new BackgroundFill(AppJoint.unselectedColor, null, null)));
        }

        // 设置背景色为选中色
        root.setBackground(new Background(new BackgroundFill(AppJoint.selectedColor, null, null)));

        // 设置活动图层id
        AppJoint.getInstance().setActiveLayerId(id);

    }

    private void actionAddedToLayerBar(int id, GridPane root) {
        String textShow = "显示";
        String textOff = "隐藏";

        setLayerActive(id, root);

        int activeId = AppJoint.getInstance().getActiveLayerId();
//
//                    if(activeId==-1){
//                        return;
//                    }

        Layer activeLayer = LayerDelegate.getInstance().getLayerById(activeId);
        if (activeLayer.getVisible()) {
            btnShowLayer.setText(textOff);
        } else {
            btnShowLayer.setText(textShow);
        }
    }


// 工具响应 ******************************************
    // 画笔工具
    @FXML
    public void toolPenClicked() {
        try {
            //GridPane root = (GridPane)NormalPen.getInstance().getToolSettings();

            tabToolParam.setContent(NormalPen.getInstance().getToolSettings());
        } catch (Exception e) {
            e.printStackTrace();
        }

        AppJoint.getInstance().setActiveTool(NormalPen.getInstance());
    }

    // 橡皮擦工具
    @FXML
    public void toolEraserClicked() {
        try {
            tabToolParam.setContent(Eraser.getInstance().getToolSettings());
        } catch (Exception e) {
            e.printStackTrace();
        }

        AppJoint.getInstance().setActiveTool(Eraser.getInstance());
    }

    // 吸管工具
    @FXML
    public void toolColorPickerClicked() {
        try {
            tabToolParam.setContent(Straw.getInstance().getToolSettings());
        } catch (Exception e) {
            e.printStackTrace();
        }

        AppJoint.getInstance().setActiveTool(Straw.getInstance());
    }

    // 随机画笔工具
    @FXML
    public void toolRandomPenClicked() {
        try {
            tabToolParam.setContent(RandomPen.getInstance().getToolSettings());
        } catch (Exception e) {
            e.printStackTrace();
        }

        AppJoint.getInstance().setActiveTool(RandomPen.getInstance());
    }

    // 减淡工具
    @FXML
    public void toolLightenClicked() {
        try {
            tabToolParam.setContent(Lighten.getInstance().getToolSettings());
        } catch (Exception e) {
            e.printStackTrace();
        }

        AppJoint.getInstance().setActiveTool(Lighten.getInstance());
    }

    // 加深工具
    @FXML
    public void toolDarkenClicked() {
        try {
            tabToolParam.setContent(Darken.getInstance().getToolSettings());
        } catch (Exception e) {
            e.printStackTrace();
        }

        AppJoint.getInstance().setActiveTool(Darken.getInstance());
    }

    // 通道画笔工具
    @FXML
    public void toolChannelPenClicked() {
        try {
            tabToolParam.setContent(ChannelPen.getInstance().getToolSettings());
        } catch (Exception e) {
            e.printStackTrace();
        }

        AppJoint.getInstance().setActiveTool(ChannelPen.getInstance());
    }

    // 灰度画笔工具
    @FXML
    public void toolGrayPenClicked() {
        try {
            tabToolParam.setContent(GrayPen.getInstance().getToolSettings());
        } catch (Exception e) {
            e.printStackTrace();
        }

        AppJoint.getInstance().setActiveTool(GrayPen.getInstance());
    }

    // 纹理画笔工具
    @FXML
    public void toolTexturePenClicked() {
        try {
            tabToolParam.setContent(TexturePen.getInstance().getToolSettings());
        } catch (Exception e) {
            e.printStackTrace();
        }

        AppJoint.getInstance().setActiveTool(TexturePen.getInstance());
    }

    // 色相画笔工具
    @FXML
    public void toolHuePenClicked() {
        try {
            tabToolParam.setContent(HuePen.getInstance().getToolSettings());
        } catch (Exception e) {
            e.printStackTrace();
        }

        AppJoint.getInstance().setActiveTool(HuePen.getInstance());
    }
// **************************************************


// 图层控制 ******************************************
    // 显示/隐藏图层
    @FXML
    public void btnShowClicked() {
        final String textShow = "显示";
        final String textOff = "隐藏";

        int activeId = AppJoint.getInstance().getActiveLayerId();

        if (activeId == -1) {
            return;
        }
        int index = LayerDelegate.getInstance().getIndexGivenId(activeId);

        Layer activeLayer = LayerDelegate.getInstance().getLayerById(activeId);

        GridPane activeLayerBar = (GridPane) vboxLayer.getChildren().get(index);
        Label labelShow = (Label) activeLayerBar.getChildren().get(2);

        if (labelShow.getText().equals(textShow)) {
            labelShow.setText(textOff);
        } else {
            labelShow.setText(textShow);
        }

        if (activeLayer.getVisible()) {
            activeLayer.setVisible(false);
        } else {
            activeLayer.setVisible(true);
        }

        LayerDelegate.getInstance().refreshAll();
        AppJoint.getInstance().flush();
    }

    // 新建图层
    @FXML
    public void btnRenameClicked(){

        int id = AppJoint.getInstance().getActiveLayerId();
        if(id==-1){
            showWarningmessage("没有活动的图层！");
            return;
        }

        backToNormal();

        FXMLLoader loader = new FXMLLoader(getClass().getResource("/View/RenameBoxWindow.fxml"));

        try{
            GridPane root = loader.load();
            if(AppJoint.getInstance().isRenameBoxActivited()){
                vboxToolBar.getChildren().remove(1);
                AppJoint.getInstance().setRenameBoxActivited(false);
                return;
            }
            vboxToolBar.getChildren().add(1,root);
            AppJoint.getInstance().setRenameBoxActivited(true);

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @FXML
    public void btnLockOnAction(){
        int id = AppJoint.getInstance().getActiveLayerId();
        if(id == -1){
            return;
        }
        Layer layer = LayerDelegate.getInstance().getLayerById(id);

        String textLocked = "锁定";
        String textUnLocked = "无锁";

        GridPane root = layer.getRoot();

        Label label = (Label)root.getChildren().get(3);

        if(label.getText().equals(textLocked)){
            layer.setLocked(false);
            label.setText(textUnLocked);
        }else{
            layer.setLocked(true);
            label.setText(textLocked);
        }
    }

    // 新建图层点击
    @FXML
    public void btnNewLayerClicked() {
        try {

            GridPane root;

            // 加载FXML
            root = FXMLLoader.load(getClass().getResource("/View/LayerBar.fxml"));

            int id = LayerDelegate.getInstance().createNewLayer("画笔图层", root);

            Label lbName = (Label)root.getChildren().get(1);
            lbName.setText("画笔图层");

            vboxLayer.getChildren().add(0, root);

            root.setBackground(new Background(new BackgroundFill(AppJoint.selectedColor, null, null)));

            setLayerActive(id, root);

            // 设置图层编号
            Label lblId = (Label) root.getChildren().get(0);
            lblId.setText("id." + Integer.toString(id));

            // 捕获点击事件
            root.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    actionAddedToLayerBar(id, root);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

        LayerDelegate.getInstance().refreshAll();
    }

    // 删除图层点击
    @FXML
    public void btnDelLayerClicked() {
        if (AppJoint.getInstance().getActiveLayerId() != -1) {
            int index = LayerDelegate.getInstance().deleteLayer(AppJoint.getInstance().getActiveLayerId());
            vboxLayer.getChildren().remove(index);
            //AppJoint.getInstance().setActiveLayerId(-1);

            if (index == 0) {
                if (vboxLayer.getChildren().size() == 0) {
                    AppJoint.getInstance().setActiveLayerId(-1);
                } else {
                    setLayerActive(LayerDelegate.getInstance().getLayers().get(0).getId(), LayerDelegate.getInstance().getLayers().get(0).getRoot());
                }
            } else {
                setLayerActive(LayerDelegate.getInstance().getLayers().get(index - 1).getId(), LayerDelegate.getInstance().getLayers().get(index - 1).getRoot());
            }
        }
    }

    // 上移一层点击
    @FXML
    public void btnMoveUpClicked() {
        if (AppJoint.getInstance().getActiveLayerId() != -1) {
            int index = LayerDelegate.getInstance().moveUp(AppJoint.getInstance().getActiveLayerId());

            if (index >= 1) {
                ObservableList<Node> list = vboxLayer.getChildren();
                Node temp = list.remove(index);
                list.add(index - 1, temp);
            }

            LayerDelegate.getInstance().refreshAll();
        }
    }

    // 下移一层点击
    @FXML
    public void btnMoveDownClicked() {
        if (AppJoint.getInstance().getActiveLayerId() != -1) {
            int index = LayerDelegate.getInstance().moveDown(AppJoint.getInstance().getActiveLayerId());

            ObservableList<Node> list = vboxLayer.getChildren();
            if (index <= list.size() - 2) {
                Node temp = list.remove(index);
                list.add(index + 1, temp);
            }
        }

        LayerDelegate.getInstance().refreshAll();
    }

    @FXML
    public void onBlendModeChanged() {
        String newBlendMode = blendMode.getValue();

        int activeLayerId = AppJoint.getInstance().getActiveLayerId();
        if(activeLayerId != -1) {
            Layer activeLayer = LayerDelegate.getInstance().getLayerById(activeLayerId);

            activeLayer.setBlendMode(BlendLayer.getBlendModeIntegerByName(newBlendMode));
            LayerDelegate.getInstance().refreshAll();
        }
    }
// **************************************************

// 滤镜 **********************************************
    // 反色
    @FXML
    public void reverseColorOnAction() {
        int id = AppJoint.getInstance().getActiveLayerId();
        if (id == -1) {
            showWarningmessage("你需要新建一个图层");
            return;
        }

        Layer layer = LayerDelegate.getInstance().getLayerById(id);

        if(layer.isLocked()){
            showWarningmessage("不能对一个锁定图层进行操作");
            return;
        }

        backToNormal();
        //Layer layer = LayerDelegate.getInstance().getLayerById(id);

        ReverseColorMirror.getInstance().antiColorMirrorActivated(layer);
    }

    // 黑白
    @FXML
    public void greyOnAction() {
        int id = AppJoint.getInstance().getActiveLayerId();
        if (id == -1) {
            showWarningmessage("你需要新建一个图层");
            return;
        }

        Layer layer = LayerDelegate.getInstance().getLayerById(id);

        if(layer.isLocked()){
            showWarningmessage("不能对一个锁定图层进行操作");
            return;
        }

        backToNormal();
        //Layer layer = LayerDelegate.getInstance().getLayerById(id);

        BlackColorMirror.getInstance().blackColorMirrorActivited(layer);
    }

    // 填充前景色
    @FXML
    public void frontColorOnAction() {
        int id = AppJoint.getInstance().getActiveLayerId();
        if (id == -1) {
            showWarningmessage("你需要新建一个图层");
            return;
        }

        Layer layer = LayerDelegate.getInstance().getLayerById(id);

        if(layer.isLocked()){
            showWarningmessage("不能对一个锁定图层进行操作");
            return;
        }
        backToNormal();
        //Layer layer = LayerDelegate.getInstance().getLayerById(id);
        FrontColorMirror.getInstance().frontColorMirrorActivited(layer);
    }

    // 填充背景色
    @FXML
    public void backColorOnAction() {
        int id = AppJoint.getInstance().getActiveLayerId();
        if (id == -1) {
            showWarningmessage("你需要新建一个图层");
            return;
        }

        Layer layer = LayerDelegate.getInstance().getLayerById(id);

        if(layer.isLocked()){
            showWarningmessage("不能对一个锁定图层进行操作");
            return;
        }
        backToNormal();
        //Layer layer = LayerDelegate.getInstance().getLayerById(id);
        BackColorMirror.getInstance().backColorMirrorActivited(layer);
    }

    // 清除颜色
    @FXML
    public void eraseColorOnAction() {
        int id = AppJoint.getInstance().getActiveLayerId();
        if (id == -1) {
            showWarningmessage("你需要新建一个图层");
            return;
        }
        Layer layer = LayerDelegate.getInstance().getLayerById(id);

        if(layer.isLocked()){
            showWarningmessage("不能对一个锁定图层进行操作");
            return;
        }
        backToNormal();
        //Layer layer = LayerDelegate.getInstance().getLayerById(id);
        EraseColorMirror.getInstance().eraseColorActivited(layer);
    }
// ***************************************************


//文件 **********************************************
    @FXML
    public void openPic() {
        try {
            GridPane root;

            WritableImage img = FileAdd.imgAdd(FileAdd.fileChoose(getPrimarStage()));

            if (img == null) {
                return;
            }

            // 加载FXML
            root = FXMLLoader.load(getClass().getResource("/View/LayerBar.fxml"));

            Label lbName = (Label)root.getChildren().get(1);
            lbName.setText("文件图层");

            int id = LayerDelegate.getInstance().createNewLayer("文件图层", root, img);

            root.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    actionAddedToLayerBar(id, root);
                }
            });

            vboxLayer.getChildren().add(0, root);

            setLayerActive(id, root);

            // 设置图层编号
            Label lblId = (Label) root.getChildren().get(0);
            lblId.setText("id." + Integer.toString(id));


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @FXML
    public void savePicOnAction() {
        Image image = AppJoint.getInstance().getOutImg().getWritableImage();

        if (image == null) {
            return;
        }

        File file;
        file = FileSave.fileChoose(getPrimarStage());

        if (file == null) {
            return;
        }

        BufferedImage bImage = SwingFXUtils.fromFXImage(image, null);
        try {
            ImageIO.write(bImage, "png", file);
            AppJoint.getInstance().setSAVED(true);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @FXML
    public void newCanvasOnAction() {
        //如果已经保存 或者是开始界面
        boolean start = AppJoint.getInstance().isStart();
        if (AppJoint.getInstance().isSAVED() || (!start)) {
            AppJoint.getInstance().clearAll();
            getMain().showNewCanvasWindow();
        } else {
            getMain().showSaveWindow();
        }
    }
// *************************************************************************

// 工具控制 ******************************************************************
    // 画布鼠标按下
    @FXML
    public void onImageViewMousePressed(MouseEvent e) {
        int activeLayerId = AppJoint.getInstance().getActiveLayerId();

        if(activeLayerId == -1) {
            showWarningmessage("你需要新建一个图层");
            return;
        }

        Layer layer = LayerDelegate.getInstance().getLayerById(activeLayerId);

        if(layer.isLocked()){
            showWarningmessage("不能对一个锁定图层进行操作");
            return;
        }

        backToNormal();

        Tool activeTool = AppJoint.getInstance().getActiveTool();

        if (activeTool != null) {
            activeTool.onMouseDown(e);
        }

    }

    // 画布鼠标移动
    @FXML
    public void onImageViewMouseMoved(MouseEvent e) {
        int activeLayerId = AppJoint.getInstance().getActiveLayerId();

        if(activeLayerId == -1) {
            showWarningmessage("你需要新建一个图层");
            return;
        }

        Layer layer = LayerDelegate.getInstance().getLayerById(activeLayerId);

        if(layer.isLocked()){
            showWarningmessage("不能对一个锁定图层进行操作");
            return;
        }

        backToNormal();

        Tool activeTool = AppJoint.getInstance().getActiveTool();

        if (activeTool != null) {
            activeTool.onMouseMove(e);
        }

    }

    // 画布鼠标放开
    @FXML
    public void onImageViewMouseReleased(MouseEvent e) {
        int activeLayerId = AppJoint.getInstance().getActiveLayerId();

        if(activeLayerId == -1) {
            showWarningmessage("你需要新建一个图层");
            return;
        }

        Layer layer = LayerDelegate.getInstance().getLayerById(activeLayerId);

        if(layer.isLocked()){
            showWarningmessage("不能对一个锁定图层进行操作");
            return;
        }

        backToNormal();

        Tool activeTool = AppJoint.getInstance().getActiveTool();

        if (activeTool != null) {
            activeTool.onMouseUp(e);
        }

    }
// ***************************************************************************

    @FXML
    public void combineLayerOnAction() {
        combineLayer(false);
    }

    @FXML
    public void combineLayerOnActionDelete() {
        combineLayer(true);
    }

    private void combineLayer(boolean delete) {
        boolean flagAllInvisible = true;

        boolean flagHasInvisible = false;

        Layer layer = new Layer("合并图层",AppJoint.getInstance().getCanvasWidth(), AppJoint.getInstance().getCanvasHeight());

        if (LayerDelegate.getInstance().getLayers().size() == 0) {
            //getMain().showWarnningWindow("你必须新建一个图层");
            showWarningmessage("你需要新建一个图层");
            return;
        }
//找出可见的图层，加入到新的图层中

        backToNormal();

        if (delete == true) {
            while (true) {
                int i;
                flagAllInvisible = true;
                for (i = LayerDelegate.getInstance().getLayers().size() - 1; i >= 0; i--) {
                    if (LayerDelegate.getInstance().getLayers().get(i).getVisible()) {
                        flagAllInvisible = false;
                        flagHasInvisible = true;
                        break;
                    }
                }
                if (LayerDelegate.getInstance().getLayers().size() == 0) {
                    break;
                }

                if (flagAllInvisible) {
                    break;
                } else {
                    for (int j = 0; j < AppJoint.getInstance().getCanvasWidth(); j++) {
                        for (int k = 0; k < AppJoint.getInstance().getCanvasHeight(); k++) {
                            //System.out.println(LayerDelegate.getInstance().getLayers().get(i).getArgb(j,k)+"sdsadsadasdasdsa");

                            if (LayerDelegate.getInstance().getLayers().get(i).getArgb(j, k) != 0) {
                                layer.setArgb(j, k, LayerDelegate.getInstance().getLayers().get(i).getArgb(j, k));
                            }
                        }
                    }

                    LayerDelegate.getInstance().getLayers().remove(i);
                    vboxLayer.getChildren().remove(i);


                }
            }
        } else {
            for (int i = LayerDelegate.getInstance().getLayers().size() - 1; i >= 0; i--) {
                if (LayerDelegate.getInstance().getLayers().get(i).getVisible()) {
                    flagHasInvisible = true;
                    for (int j = 0; j < AppJoint.getInstance().getCanvasWidth(); j++) {
                        for (int k = 0; k < AppJoint.getInstance().getCanvasHeight(); k++) {
                            //System.out.println(LayerDelegate.getInstance().getLayers().get(i).getArgb(j,k)+"sdsadsadasdasdsa");

                            if (LayerDelegate.getInstance().getLayers().get(i).getArgb(j, k) != 0) {
                                layer.setArgb(j, k, LayerDelegate.getInstance().getLayers().get(i).getArgb(j, k));
                            }
                        }
                    }
                }
            }
        }

        if (flagHasInvisible) {
            try {
                LayerDelegate.getInstance().getLayers().add(0, layer);

                GridPane root = FXMLLoader.load(getClass().getResource("/View/LayerBar.fxml"));

                int id = layer.getId();

                layer.setRoot(root);

                vboxLayer.getChildren().add(0, root);

                setLayerActive(id, root);

                // 设置图层编号
                Label lblId = (Label) root.getChildren().get(0);
                lblId.setText("id." + Integer.toString(id));

                Label lbName = (Label) root.getChildren().get(1);
                lbName.setText("合并图层");

                // 捕获点击事件
                root.setOnMouseClicked(new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent event) {
                        actionAddedToLayerBar(id, root);
                    }
                });

                LayerDelegate.getInstance().refreshAll();
                AppJoint.getInstance().flush();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @FXML
    private MenuItem antiColorText;

    @FXML
    public void mirrorOnAction() {
        String nameAntiColor = "反色";
        String on = "(打开)";
        String off = "(关闭)";
    }

    //前景色后景色的设置

    @FXML
    ColorPicker frontColorPicker;

    @FXML
    ColorPicker backColorPicker;


    public ColorPicker getFrontColorPicker() {
        return frontColorPicker;
    }

    public Rectangle getFrontColorBox() {
        return frontColorBox;
    }

    public ColorPicker getBackColorPicker() {
        return backColorPicker;
    }

    public Rectangle getBackColorBox() {
        return backColorBox;
    }

    //左下角的矩形
    @FXML
    Rectangle frontColorBox;

    @FXML
    Rectangle backColorBox;

    //选择前景色
    @FXML
    public void frontColorPickerChanged() {
        int r = (int) (frontColorPicker.getValue().getRed() * 255);
        int g = (int) (frontColorPicker.getValue().getGreen() * 255);
        int b = (int) (frontColorPicker.getValue().getBlue() * 255);


//        System.out.println(r);
//        System.out.println(g);
//        System.out.println(b);

        int argb = (0xff << 24) | (r << 16) | (g << 8) | b;

        AppJoint.getInstance().setFrontColorArgb(argb);
        frontColorBox.setFill(Color.rgb(r, g, b));

    }

    //选择后景色
    @FXML
    public void backColorPickerChanged() {
        int r = (int) (backColorPicker.getValue().getRed() * 255);
        int g = (int) (backColorPicker.getValue().getGreen() * 255);
        int b = (int) (backColorPicker.getValue().getBlue() * 255);

        int argb = (0xff << 24) | (r << 16) | (g << 8) | b;

        AppJoint.getInstance().setBackColorArgb(argb);

        backColorBox.setFill(Color.rgb(r, g, b));
    }

    //penSetting的colorpicker
    public void setColorPicker(ColorPicker colorPicker) {
        this.colorPicker = colorPicker;
    }

    private ColorPicker colorPicker;

    //点击左下角的时候
    @FXML
    public void frontColorBoxClicked() {

        //将前景色注入画笔
        Tool activeTool = AppJoint.getInstance().getActiveTool();
        if (activeTool == null) {
            return;
        }
        if (activeTool.getClass().getSimpleName().equals("NormalPen")) {
            NormalPen normalPen = (NormalPen) activeTool;

            int argb = AppJoint.getInstance().getFrontColorArgb();

            int a = (argb >> 24) & (0xff);
            int r = (argb >> 16) & (0xff);
            int g = (argb >> 8) & (0xff);
            int b = (argb) & (0xff);
            normalPen.setColor(a, r, g, b);
            //PenSettingsController.getInstance().getPenSettingsColor().setValue(Color.rgb(r,g,b));
            colorPicker.setValue(Color.rgb(r, g, b));
//            tabToolParam.setContent(NormalPen.getInstance().getToolSettings(Color.rgb(r,g,b)));
            System.out.print(colorPicker.getValue());
        }
    }

    @FXML
    public void backColorBoxClicked() {

        Tool activeTool = AppJoint.getInstance().getActiveTool();
        boolean flagNull = false;
        if (activeTool == null) {
            flagNull = true;
        }
        if (!flagNull) {
            if (activeTool.getClass().getSimpleName().equals("NormalPen")) {
                NormalPen normalPen = (NormalPen) activeTool;

                int argb = AppJoint.getInstance().getBackColorArgb();

                int a = (argb >> 24) & (0xff);
                int r = (argb >> 16) & (0xff);
                int g = (argb >> 8) & (0xff);
                int b = (argb) & (0xff);
                normalPen.setColor(a, r, g, b);

                colorPicker.setValue(Color.rgb(r, g, b));
            }
        } else {
            return;
        }

        //交换前后景色

        int frontArgb = AppJoint.getInstance().getFrontColorArgb();
        int backArgb = AppJoint.getInstance().getBackColorArgb();

        int forntA = (frontArgb >> 24) & (0xff);
        int frontR = (frontArgb >> 16) & (0xff);
        int frontG = (frontArgb >> 8) & (0xff);
        int frontB = (frontArgb) & (0xff);

        int backA = (backArgb >> 24) & (0xff);
        int backR = (backArgb >> 16) & (0xff);
        int backG = (backArgb >> 8) & (0xff);
        int backB = (backArgb) & (0xff);

        AppJoint.getInstance().setFrontColorArgb(backArgb);
        AppJoint.getInstance().setBackColorArgb(frontArgb);

        frontColorPicker.setValue(Color.rgb(backR, backG, backB));
        backColorPicker.setValue(Color.rgb(frontR, frontG, frontB));

        frontColorBox.setFill(Color.rgb(backR, backG, backB));
        backColorBox.setFill(Color.rgb(frontR, frontG, frontB));
    }


    //上方菜单栏
    @FXML
    public void menuHideLayerOnAction(){
        btnShowClicked();
    }

    @FXML
    public void menuDelLayerOnAction(){
        btnDelLayerClicked();
    }

    @FXML
    public void menuNewLayerOnAction(){
        btnNewLayerClicked();
    }

    @FXML
    public void menuRenameOnAction(){
        btnRenameClicked();
    }

    @FXML
    public void menuLockLayerOnAction(){
        btnLockOnAction();
    }

    // ***************************************************************************


    @FXML private ComboBox<String> blendMode;

    public ComboBox<String> getCombo() {
        return blendMode;
    }

    private void showWarningmessage(String name){
        StatusBarManager.getInstance().setBackgroundColor(StatusBarManager.COLOR_ERROR);
        StatusBarManager.getInstance().setText(name);
    }

    private void backToNormal(){
        StatusBarManager.getInstance().setBackgroundColor(StatusBarManager.COLOR_NORMAL);
        StatusBarManager.getInstance().setText("");
    }
}